using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class _Default : System.Web.UI.Page
    {
        public int CateID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            Title.TitleName = ResourceManager.GetString("Product_ShoppingCart");

            if (Globals.CurrentUserName == "")
                Response.Redirect(Globals.ApplicationPath +"Logon.aspx?Action=addtocart");
            
            if(Globals.GetStringFromQueryString("Action").ToLower() =="addtocart")
                if(!Page.IsPostBack)
                    AddToCart(Globals.GetIntFromQueryString("ProductID"));

            if (GridViewItem.Rows.Count <= 0)
                PanelUpdate.Visible = false;
        }



        private void AddToCart(int ProductID)
        { 
            Products obj = Products.GetProductsByID(ProductID);
            ShoppingCart shop = new ShoppingCart();
            shop.UserID = Globals.CurrentUserID;
            shop.AddedDate = DateTime.Now;
            shop.ProductID = obj.ProductID;
            shop.Title = obj.Title;
            shop.UnitPrice = obj.UnitPrice;
            shop.Quantity = 1;
            shop.Subtotal = obj.UnitPrice;
            CateID = obj.CateID;
            ShoppingCart.Insert(shop);
        }


        protected void btUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < GridViewItem.Rows.Count; i++)
                {
                    GridViewRow row = GridViewItem.Rows[i];

                    int Quantity = Convert.ToInt32(((TextBox)row.FindControl("txtQuantity")).Text);
                    int ID = Convert.ToInt32(((CheckBox)row.FindControl("chkSelect")).ToolTip);

                    // xoa don hang nay
                    if (Quantity <= 0)
                        ShoppingCart.Delete(ID);
                    else
                    {
                        ShoppingCart shop = new ShoppingCart();
                        shop.UserID = Globals.CurrentUserID;
                        shop.ProductID = ID;
                        shop.Quantity = Quantity;
                        shop.Subtotal = 1;
                        ShoppingCart.Update(shop);
                    }
                }
                GridViewItem.DataBind();
            }
            catch { }
        }


        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridViewItem.Rows.Count; i++)
            {
                GridViewRow row = GridViewItem.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int ID = Convert.ToInt32(((CheckBox)row.FindControl("chkSelect")).ToolTip);
                    ShoppingCart.Delete(ID);
                }
            }
            GridViewItem.DataBind();
        }

    }
}